package com.slatch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Slatch slatch;
	public static MainView view;
	public static String map;
	public static String doublevai;
	public static String maptest;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		loadDataFromAssetdoublevai();
		loadDataFromAssetmaptest();
		// Choix de la map
		map=maptest;
		slatch = new Slatch();
		view = new MainView(this); 
	    setContentView(view);
	}


	public void loadDataFromAssetdoublevai() {
        try {
            InputStream is = getAssets().open("doublevai.txt");
            doublevai=convertStreamToString(is);
        }
        catch (IOException ex) {
            return;
        }
    }
	
	public void loadDataFromAssetmaptest() {
        try {
            InputStream is = getAssets().open("mapTest4.txt");
            maptest=convertStreamToString(is);
        }
        catch (IOException ex) {
            return;
        }
    }

	
    public String convertStreamToString(InputStream p_is) throws IOException {
        if (p_is != null) {
            StringBuilder m_sb = new StringBuilder();
            String m_line;
            try {
                BufferedReader m_reader = new BufferedReader(
                        new InputStreamReader(p_is));
                while ((m_line = m_reader.readLine()) != null) {
                	m_sb.append(m_line).append("_");
                    //m_sb.append("|");
                }
            } finally {
                p_is.close();
            }
            //Log.e("TAG", m_sb.toString());
            return m_sb.toString();
        } else {
            return "";
        }
    }
	
	/**
     * Invoked when the Activity loses user focus.
     */
    @Override
    protected void onPause() {
        super.onPause();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    }


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
    public boolean onTouchEvent(MotionEvent event) {
    	try {
    		view.onTouch(event);
    	} catch(Exception e) {}
    	return true;
    }

	public static void setMaView(MainView maView) {
		MainActivity.view = maView;
	}
}

